package com.ybt.heartratem.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ListView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.ybt.heartratem.R;


public class CustomSwipeToRefresh extends SwipeRefreshLayout {
    private final int mScaledTouchSlop;
    private final View mFooterView;
    private ListView mListView;
    private OnLoadMoreListener mListener;
    private boolean isLoading;
    private RecyclerView mRecyclerView;
    private float mDownY, mUpY;

    public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
        super(context, attrs);
        mFooterView = View.inflate(context, R.layout.view_footer, null);
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mListView == null || mRecyclerView == null) {
            if (getChildCount() > 0) {
                if (getChildAt(0) instanceof ListView) {
                    mListView = (ListView) getChildAt(0);
                } else if (getChildAt(0) instanceof RecyclerView) {
                    mRecyclerView = (RecyclerView) getChildAt(0);
                }
            }
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (canLoadMore()) {
                    loadData();
                }
                break;
            case MotionEvent.ACTION_UP:
                mUpY = ev.getY();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean canLoadMore() {
        boolean condition1 = (mDownY - mUpY) >= mScaledTouchSlop;
        if (condition1) {
        }

        boolean condition2 = false;
        if (mListView != null && mListView.getAdapter() != null) {
            condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
        }

        if (condition2) {
        }
        boolean condition3 = !isLoading;
        if (condition3) {
        }
        return condition1 && condition2 && condition3;
    }

    private void loadData() {
        if (mListener != null) {
            setLoading(true);
            mListener.onLoadMore();
        }
    }

    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            mListView.addFooterView(mFooterView);
        } else {
            if (mListView != null && mFooterView != null) {
                mListView.removeFooterView(mFooterView);
                mDownY = 0;
                mUpY = 0;
            }
        }
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener listener) {
        this.mListener = listener;
    }
}
